#include <InteractionWithUIFunctions.h>
#include <osgViewer/Viewer>
#include <osgQt/GraphicsWindowQt>

#include <SceneViewerFunctions.h>
#include <QDateTime>
using namespace InteractionWithUIFunctions;


QWidget* InteractionWithUIFunctions::getSceneViewerWidget()
{
	osg::DisplaySettings* ds =
		osg::DisplaySettings::instance().get();
	osg::ref_ptr<osg::GraphicsContext::Traits> traits =
		new osg::GraphicsContext::Traits;
	traits->windowDecoration = false;
	traits->x = 0;
	traits->y = 0;
	traits->width = 640;
	traits->height = 480;
	traits->doubleBuffer = true;
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setGraphicsContext(
		new osgQt::GraphicsWindowQt(traits.get()) );
	camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
	camera->setViewport( new osg::Viewport(
		0, 0, traits->width, traits->height) );
	camera->setProjectionMatrixAsPerspective(
		30.0f, static_cast<double>(traits->width)/
		static_cast<double>(traits->height), 1.0f, 10000.0f );
	//_viewer->setGraphicsContext(camera->getGraphicsContext());

	SceneViewerFunctions::getOsgCamera()->setGraphicsContext(camera->getGraphicsContext());

	osgQt::GraphicsWindowQt* gw1 = dynamic_cast<osgQt::GraphicsWindowQt*>( SceneViewerFunctions::getOsgCamera()->getGraphicsContext());
	return gw1->getGLWidget();

}

void InteractionWithUIFunctions::sleep(int msec)
{
    QDateTime last = QDateTime::currentDateTime();
    QDateTime now;
    while (1)
    {
        now = QDateTime::currentDateTime();
        if (last.msecsTo(now) >= msec)
        {
            break;
        }
    }
}
